package cn.itsource.pethome.user.service.impl;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.user.domain.Logininfo;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.user.domain.dto.UserDTO;
import cn.itsource.pethome.user.mapper.LogininfoMapper;
import cn.itsource.pethome.user.mapper.UserMapper;
import cn.itsource.pethome.user.service.IUserService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.util.BusinessException;
import cn.itsource.pethome.util.MD5Utils;
import cn.itsource.pethome.util.StrUtils;
import javafx.scene.control.TextArea;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author majun
 * @since 2022-10-13
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper ;
    @Autowired
    private LogininfoMapper logininfoMapper ;
    @Autowired
    JavaMailSender mailSender;

    /**
     * 用户电话号注册
     * @param userDTO
     */
    @Override
    public void register(UserDTO userDTO) {
        //注册功能的实现
        //1.验证手机号是否为null
        if(StringUtils.isEmpty(userDTO.getPhone())){
            throw new BusinessException("电话号码不能为空！！");
        }
        //2.手机验证码是否为null
        if(StringUtils.isEmpty(userDTO.getCode())){
            throw new BusinessException("手机验证码不能为空！！");
        }
        //3.密码是否为null
        if(StringUtils.isEmpty(userDTO.getPassword())){
            throw new BusinessException("密码不能为空！！");
        }

        //4.确认密码是否为null
        if(StringUtils.isEmpty(userDTO.getPasswordRepeat())){
            throw new BusinessException("确认密码不能为空！！");
        }
        //5.密码和确认密码是否一致
        if(!userDTO.getPassword().equals(userDTO.getPasswordRepeat())){
            throw new BusinessException("密码和确认密码不一致");
        }
        //6.手机号是否被注册过
        User user = userMapper.QueryByPhone(userDTO.getPhone());
        if(user != null ){
            throw new BusinessException("手机号已被注册过,请登录！");
        }
        //7.根据手机号+业务键 去redis中获取手机验证
        Object values = redisTemplate.opsForValue().get(userDTO.getPhone() + BaseConstants.register.REGISTER);
        //7.2 无
        //7.2.1 直接抛出 验证已失效
        if(values ==null){
            throw new BusinessException("验证已失效");
        }else {//7.1 有
            //7.1.1 判断两个验证码是否一致
            if(!values.toString().split(":")[0].equalsIgnoreCase(userDTO.getCode())){
                throw new BusinessException("验证码输入错误！！");
            }
            //7.1.2 将数据保存到数据库
            User user1 = new User();
            user1.setUsername(userDTO.getPhone());
            user1.setPhone(userDTO.getPhone());
            user1.setPassword(userDTO.getPassword());
            //设置盐值
            val md5 = MD5Utils.encrypByMd5(userDTO.getPassword());
            user1.setState(BaseConstants.User.STATE_ACTIVATED);
            //7.1.3 将logininfo也保存一份
            this.insert(user1);
        }

    }
    /**
     * 用户邮箱注册
     * @param userDTO
     */
    @Override
    public void register1(UserDTO userDTO) {
        //注册功能的实现
        //1.验证邮箱是否为null
        if(userDTO.getPhone()==null){
            throw new BusinessException("电话号码不能为空！！");
        }
        //2.密码是否为null
        if(userDTO.getPassword()==null){
            throw new BusinessException("密码不能为空！！");
        }
        //4.确认密码是否为null
        if(userDTO.getPasswordRepeat()==null){
            throw new BusinessException("确认密码不能为空！！");
        }
        //5.密码和确认密码是否一致
        if(!userDTO.getPassword().equals(userDTO.getPasswordRepeat())){
            throw new BusinessException("密码和确认密码不一致");
        }
        //6.邮箱是否被注册过
        User user = userMapper.QueryByemail(userDTO.getEmail());
        if(user != null ){
            throw new BusinessException("手机号已被注册过,请登录！");
        }

            //7.1.2 将数据保存到数据库
            User user1 = new User();
            user1.setUsername(userDTO.getEmail());
            user1.setPhone(userDTO.getEmail());
            user1.setPassword(userDTO.getPassword());
            user1.setState(BaseConstants.User.STATE_ACTIVATED);

            //7.1.3 将logininfo也保存一份
            this.insert(user1);
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            //使用工具类来构建消息
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //
            helper.setFrom("1263913263@qq.com");
            //设置邮件主题
            helper.setSubject("mj宠物之家审核通知");
            //邮件正文
            helper.setText("<h1>注册成功</h1><br>"+
                    "<a href='http://127.0.0.1:8081/#/register2?id="+user1.getId()+"'>点击用户激活</a>",true);
            //发送人
            helper.setTo(user1.getEmail());
            //发送消息
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }



    }

    @Override
    public void insert(User user) {
        //设置加密并存入数据库中
        this.encryption(user);
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setDisable(1);
        logininfo.setType(1);
        logininfoMapper.insert(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.insert(user);
    }

    /**
     * 设置加密
     * @param user
     */
    private void encryption(User user){
        //创建盐值
         String randomString = StrUtils.getComplexRandomString(32);
        //把盐值添加到user中
        user.setSalt(randomString);
        //给密码设置加密
        String pasWd = MD5Utils.encrypByMd5(user.getPassword() + randomString);
        //存入数据库中
        user.setPassword(pasWd);
    }
}
